﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>酒店管理-员工管理</title>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="stylesheet" href="assets/materialize/css/materialize.min.css" media="screen,projection" />
    <!-- Bootstrap Styles-->
    <link href="assets/css/bootstrap.css" rel="stylesheet" />
    <!-- FontAwesome Styles-->
    <link href="assets/css/font-awesome.css" rel="stylesheet" />
    <!-- Morris Chart Styles-->
    <link href="assets/js/morris/morris-0.4.3.min.css" rel="stylesheet" />
    <!-- Custom Styles-->
    <link href="assets/css/custom-styles.css" rel="stylesheet" />
    <!-- Google Fonts-->
    <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' />
    <link rel="stylesheet" href="assets/js/Lightweight-Chart/cssCharts.css">
    <!-- Sweet Alert-->
    <link rel="stylesheet" href="assets/css/sweetalert2.min.css">
    <style>
        th,
        td {
            text-align: center
        }

        #btnzmx {
            margin-bottom: 20px;
            float: right;
        }

        #basic{
            width:50%;
            float: left;
        }
        #photo{
            width:50%;
            float: left;
        }
        #photo figure{
            width:200px;
            height:200px;
            border:1px solid;
            position: relative;
        }
        #photo figcaption{
            top: 0;
            width: 100%;
            height: 100%;
            line-height: 200px;
            font-size: 20px;
            color:#ccc;
            text-align: center;
            position: absolute;
        }

        #photo img{
            width:100%;
            max-height: 100%;
        }

        #myFile{
            margin-top: 10px;
            width:200px;
        }
    </style>
</head>

<body>
    <div id="wrapper">
        <nav class="navbar navbar-default top-navbar" role="navigation">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle waves-effect waves-dark" data-toggle="collapse" data-target=".sidebar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand waves-effect waves-dark" href="index.html">
                    <i class="large material-icons">track_changes</i>
                    <strong>Hotel Manage</strong>
                </a>

                <div id="sideNav" href="">
                    <i class="material-icons dp48">toc</i>
                </div>
            </div>

            <ul class="nav navbar-top-links navbar-right">
                <li>
                    <a class="dropdown-button waves-effect waves-dark" href="#!" data-activates="dropdown1">
                        <i class="fa fa-user fa-fw"></i>
                        <b id='admin'></b>
                        <i class="material-icons right">arrow_drop_down</i>
                    </a>
                </li>
            </ul>
        </nav>
        <!-- Dropdown Structure -->
        <ul id="dropdown1" class="dropdown-content">
            <li>
                <a href="#">
                    <i class="fa fa-user fa-fw"></i> 个人中心</a>
            </li>
            <li>
                <a href="#">
                    <i class="fa fa-gear fa-fw"></i> 设置</a>
            </li>
            <li id='logoutBtn'>
                <a href="#">
                    <i class="fa fa-sign-out fa-fw"></i> 退出登录</a>
            </li>
        </ul>
        <!--/. NAV TOP  -->
        <!--/. NAV TOP  -->
        <nav class="navbar-default navbar-side" role="navigation">
            <div class="sidebar-collapse">
                <ul class="nav" id="main-menu">
                    <li>
                        <a class="waves-effect waves-dark" href="index.html">
                            <i class="fa fa-dashboard"></i> 首页</a>
                    </li>
                    <li>
                        <a href="employee.html" class="active-menu waves-effect waves-dark">
                            <i class="fa fa-desktop"></i> 员工管理</a>
                    </li>
                    <li>
                        <a href="room.html" class="waves-effect waves-dark">
                            <i class="fa fa-bar-chart-o"></i> 客房管理</a>
                    </li>
                    <li>
                        <a href="role.html" class="waves-effect waves-dark">
                            <i class="fa fa-qrcode"></i>角色管理</a>
                    </li>

                    <li>
                        <a href="user.html" class="waves-effect waves-dark">
                            <i class="fa fa-table"></i>用户管理</a>
                    </li>
                </ul>

            </div>

        </nav>
        <!-- /. NAV SIDE  -->
        <div id="page-wrapper">
            <div class="header">
                <h1 class="page-header">
                    员工管理
                </h1>
                <ol class="breadcrumb">
                    <li>
                        <a href="#">主页</a>
                    </li>
                    <li>
                        <a href="#">用户管理 </a>
                    </li>
                    <li class="active">数据</li>
                </ol>

            </div>
            <div id="page-inner">
                <div id="basic">
                    <div>
                        姓名
                        <input id="username" placeholder="请输入员工姓名" style="width: 270px;" class="person">
                    </div>
                    <div>
                        <div class="radio">
                                性别&nbsp;
                            <label>
                                <input type="radio" name="sex" id="man" value="男" checked class="person"> 男
                            </label>
                            <label>
                                    <input type="radio" name="sex" id="woman" value="女"class="person"> 女
                            </label>
                        </div>
                    </div>
                    <div>
                        年龄
                        <input id="age" placeholder="请输入年龄" style="width: 270px;" type="number" max="120" min="18" class="person">
                    </div>
                    <div>
                        电话
                        <input id="phone" placeholder="请输入电话" style="width: 270px;" type="number" class="person">
                    </div>
                    <div>
                        证件
                        <input id="idCard" placeholder="请输入身份证号" style="width: 270px;" class="person">
                    </div>
                    <div class="form-inline">
                        角色
                        <select  class="form-control" style="width:270px;" id="roles"></select>
                    </div>
                    <br>
                    <div>
                        <button id="addBtn" class="btn btn-info" style="width:310px">提交</button>
                    </div>
                </div>
                <div id="photo">
                    <figure>
                        <img>
                        <figcaption>图片拖拽至此</figcaption>
                    </figure>
                    <input type="file" id="myFile">
                </div>
            </div>

            <!-- /. PAGE INNER  -->
        </div>
        <!-- /. PAGE WRAPPER  -->
    </div>
    <!-- /. WRAPPER  -->
    <!-- JS Scripts-->
    <!-- jQuery Js -->
    <script src="assets/js/jquery-1.10.2.js"></script>

    <!-- Bootstrap Js -->
    <script src="assets/js/bootstrap.min.js"></script>

    <script src="assets/materialize/js/materialize.min.js"></script>

    <!-- Custom Js -->
    <script src="assets/js/custom-scripts.js"></script>
    <script src="assets/js/public.js"></script>
    <!-- Sweet Alert -->
    <script src="assets/js/sweetalert2.min.js"></script>
    <script>
        //加载角色列表
        $.ajax({
                url: 'roles',
                method: "GET",
                success: function (list) {
                    if(list.length>0){
                        list.forEach(function(value,index){
                            $(`<option value="${value}">${value}</option>`).appendTo('#roles');
                        })
                    }else{
                        $(`<option>暂无数据</option>`).appendTo('#roles');
                    }
                }
            })

        //头像上传
        $("#photo figure")[0].ondragover=function(e){
                e.preventDefault();
            }
        $("#photo figure")[0].ondrop=function(e){
                e.preventDefault();
                var files = e.dataTransfer.files;
                $("#myFile")[0].files=files;
                displayPhoto(files);
            }
        $('#myFile').on('change',function(){
            displayPhoto(this.files);
        })

        function displayPhoto(files){
            for(var i=0;i<files.length;i++){
                    var reader=new FileReader();
                    reader.readAsDataURL(files[i]);
                    reader.onload=function(data){
                        $("#photo figcaption").hide();
                        $("#photo  img").attr('src',data.target.result);
                    }
            }
        }

        //提交新员工数据
        $('#addBtn').click(function () {
            var isValid = true;
            $('.person').each(function(i,v){
                if($(v).val().trim()=='') {
                    swal('验证错误','请将员工信息填写完整','error');
                    isValid = false;
                    return false;
                }
            })
            if(!isValid)return;
            var sex= $(':radio:checked').val();
            var myform=new FormData();
            myform.append('id',param.substring(param.indexOf('=')+1));
            myform.append('username',username.value);
            myform.append('sex',sex);
            myform.append('age',age.value);
            myform.append('phone',phone.value);
            myform.append('idCard',idCard.value);
            myform.append('role',roles.value);
            myform.append('photo',myFile.files[0]);
            $.ajax({
                url: 'addEmployee',
                method: "POST",
                processData:false,//告诉jQuery不要去处理发送的数据
                contentType:false,//告诉jQuery不要去设置Content-Type请求头
                data: myform,
                success: function (data) {
                    if (data == 'success') {
                        location.href = 'employee.html';
                    } else {
                        swal('录入失败!','系统繁忙,请稍后再试','error');
                    }
                }
            })
        })

        //根据URL来判断是修改还是新增
        var param=location.search;
        if(param){
            var id=param.substring(param.indexOf('=')+1);
            $.ajax({
                url: 'queryEmployeeById',
                method: "GET",
                data: `id=${id}`,
                success: function (emp) {
                    if (emp) {
                        $("#username").val(emp.name);
                        $(`:radio[value=${emp.sex}]`).prop('checked',true);
                        $("#age").val(emp.age);
                        $("#phone").val(emp.phone);
                        $("#idCard").val(emp.idCard);
                        $("#roles").val(emp.role);
                        $("#photo img").attr('src','assets/img/'+emp.photo);
                        $("#photo figcaption").hide();
                    } else {
                        swal('添加失败!','系统繁忙,请稍后再试','error');
                    }
                }
            })
        }
    </script>
</body>

</html>